Skip to content

[GH-2409] Clip raster to the ROI extent in zonal statistics - #3461

Open
modi3011 wants to merge 3 commits into
apache:masterfrom
modi3011:GH-2409-zonal-stats-clip
Open

modi3011 wants to merge 3 commits into
apache:masterfrom
modi3011:GH-2409-zonal-stats-clip

Conversation

@modi3011

@modi3011 modi3011 commented Oct 14, 2026 •

Copy link
Copy Markdown

Did you read the Contributor Guide?

Is this PR related to a ticket?

What changes were proposed in this PR?

getStatObjects (used by RS_ZonalStats and RS_ZonalStatsAll) rasterized the ROI at the full extent of the input raster, read two full-size sample arrays, and scanned every pixel, so the cost grew with the raster rather than with the region of interest.

This PR rasterizes only the window covered by the ROI (useGeometryExtent=true) and reads just that window from the source band.

  • The window's pixel offset in the raster is computed from the raster and window envelopes.
  • The previous full-extent path is used when the window cannot be placed exactly (a skewed raster, or a window that is not aligned to the pixel grid or does not fit inside the raster) or is smaller than one pixel. Results in those cases are unchanged. The sub-pixel case covers a polygon that only grazes a tile corner, which RasterizationTest exercises.
  • A null rasterization result (the geometry burns no pixel) yields an empty pixel set, as before.

Pixels are still visited in row-major order, so the output is unchanged.

How was this patch tested?

  • Added testZonalStatsSmallPolygonOnLargerRaster, testZonalStatsPolygonPartlyOutsideRaster, testZonalStatsNoDataInsideClippedWindow and testZonalStatsPolygonOnlyTouchingRasterBoundary to RasterBandAccessorsTest. They use a raster with distinct pixel values and a window that does not start at the raster's origin, and cover top-down and bottom-up rasters, allTouched true and false, and nodata.
  • The new tests also pass against the previous implementation, so results are unchanged.
  • RasterBandAccessorsTest and RasterizationTest pass.

Did this PR include necessary documentation updates?

  • No, this PR does not need documentation updates. It is an internal performance change with no change to function signatures or results.

getStatObjects rasterized the zone at the full extent of the input raster
and then read and scanned every pixel, so the cost scaled with raster size
rather than with the size of the region of interest. For a small polygon on a
large raster this is both slow and memory-hungry (two full-size sample
arrays).

Rasterize the ROI with useGeometryExtent=true so only the window covered by
the geometry is produced, and read just that window from the source band.
The window's offset in the source raster is computed from pixel centres. If
the window cannot be placed exactly (for example a skewed raster or a window
that does not fit inside the raster), fall back to the previous full-extent
path, so results are unchanged in those cases. A null rasterization result
(the geometry burns no pixel) now yields an empty pixel set, matching the
previous behaviour.

Statistics are unchanged: pixels are still visited in row-major order, so
the output of RS_ZonalStats and RS_ZonalStatsAll is identical to before.

Tests:
- [new tests: small polygon on a larger raster, bottom-up raster, polygon
  that only touches the raster boundary]
- RasterBandAccessorsTest passes

Benchmark ([machine, JDK], [size] raster, ~50x50 pixel polygon):
before [x] ms, after [y] ms

Closes apache#2409
@modi3011
modi3011 requested a review from jiayuasu as a code owner October 14, 2026 11:34
@jiayuasu

jiayuasu commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

@modi3011 test failed, please take a look

@modi3011

Copy link
Copy Markdown
Author

Hi @jiayuasu. Sorry for the failing tests in the last commit. Can you please take another look at the latest commit?

@sudhamsh-git

Copy link
Copy Markdown

Nice improvement. Rasterizing only the ROI's window instead of the whole raster means zonal stats now scale with the size of the zone rather than the raster, which is exactly what #2409 asks for. Thanks for the quick fix on the RasterizationTest cases too.

@jiayuasu

Copy link
Copy Markdown
Member

@modi3011 can you check your pr description? It has lots of place holder and does not make sense.

@jiayuasu

Copy link
Copy Markdown
Member

and your pr does not have any test. not sure if this is expected

Cover a small polygon on a larger raster (top-down and bottom-up), a polygon partly outside the raster, nodata exclusion inside the window, and a polygon that only touches the raster boundary.
@modi3011

Copy link
Copy Markdown
Author

Thanks @jiayuasu for pointing this out. I apologize for the frequent changes related to the PR and the description. I've fixed the description.

I've also added tests to RasterBandAccessorsTest covering a small polygon on a larger raster (top-down and bottom-up), a polygon partly outside the raster, nodata inside the window, and a polygon that only touches the boundary. They also pass against the previous implementation, so the results are unchanged.

@Mayukh-D

Copy link
Copy Markdown
Contributor

Nice, the tests cover the edge cases, including the polygon only touching the boundary and the one partly outside the raster. lgtm

@sudhamsh-git

Copy link
Copy Markdown

Thanks for adding these. Running the new tests against the previous implementation as well is a nice touch, since it shows the clipped window gives exactly the same results as before, and the bottom-up raster and allTouched cases make the coverage solid. The updated description reads clearly too. Looks good to me 👍

@jiayuasu

Copy link
Copy Markdown
Member

after reviewing this pr, i think there are a number of correctness issue of this pr. Please do not work on issues that are not marked as good first issue. Thanks.

@modi3011

Copy link
Copy Markdown
Author

Thanks for taking the time to review the PR.

I know that there may be some correctness issues that need to be addressed. I’ll make sure to focus only on issues marked as good first issues and avoid working on anything outside that scope.

I’d also appreciate it if you could point out what I could have done differently while working on this PR, or any specific areas where I could improve my approach. I’d love to learn from this experience and do better in future contributions.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Sedona Core] Sedona doesn't clip raster to geometry extent in zonal statistics which can lead to inefficient queries

4 participants